const DEFAULT_API_BASE_URL = ''

type AppConfig = {
  apiBaseUrl: string
  appName: string
  enableMock: boolean
}

let cachedConfig: AppConfig | null = null

const parseBoolean = (value: string | boolean | undefined, fallback = false) => {
  if (typeof value === 'boolean') return value
  if (typeof value !== 'string') return fallback
  return ['true', '1', 'yes', 'on'].includes(value.toLowerCase())
}

export const getConfig = (): AppConfig => {
  if (cachedConfig) return cachedConfig

  const {
    VITE_API_BASE_URL,
    VITE_APP_NAME = 'React Admin Starter',
    VITE_ENABLE_MOCK,
  } = import.meta.env

  cachedConfig = {
    apiBaseUrl: VITE_API_BASE_URL ?? DEFAULT_API_BASE_URL,
    appName: VITE_APP_NAME,
    enableMock: parseBoolean(VITE_ENABLE_MOCK, true),
  }

  return cachedConfig
}


